package com.elf.stepcounter;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;

import com.elf.stepcounter.databinding.ActivitySettingBinding;
import com.elf.zero.app.ActivityResult;
import com.elf.zero.utils.AppUtils;

public class SettingActivity extends AppBaseActivity {

    private ActivitySettingBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_setting);

        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(R.string.setting);

        binding.tvNotification.setOnClickListener(v -> notification());
        binding.tvBattleSetting.setOnClickListener(v -> batterySetting());
        binding.tvBackgroundSetting.setOnClickListener(v -> backgroundSetting());

        if (isIgnoringBatteryOptimizations()) {
            binding.tvBattleSetting.setValue("已开启");
        }

        binding.tvVersion.setValue(AppUtils.getVersionName());
    }

    private boolean isIgnoringBatteryOptimizations() {
        if (Build.VERSION.SDK_INT >= 23) {
            String pageName = getPackageName();

            PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
            return powerManager.isIgnoringBatteryOptimizations(pageName);
        }
        return false;
    }

    private void notification() {
        AppUtils.openNotificationSetting();
    }

    private void batterySetting() {
        if (Build.VERSION.SDK_INT >= 23) {
            String pageName = getPackageName();

            PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (powerManager.isIgnoringBatteryOptimizations(pageName)) {
//                    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
                showToast("已开启");
            } else {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + pageName));
                ActivityResult.with(this)
                        .listener(new ActivityResult.ActivityResultListener() {
                            @Override
                            public void resultOK(Intent data) {
                                if (isIgnoringBatteryOptimizations()) {
                                    binding.tvBattleSetting.setValue("已开启");
                                }
                            }
                        })
                        .startActivity(intent);
            }
        } else {
            showDialog("Android 6.0 以下系统无需设置此项");
        }
    }

    private void backgroundSetting() {
        AppUtils.openSetting();
    }

    public static void open(Context context) {
        context.startActivity(new Intent(context, SettingActivity.class));
    }
}